package net.sx.minemodule.widget;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.StyleRes;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.kedll.library.util.UIUtil;


/**
 * 描述:  popup
 * author:胡邦茂
 * CreateDate: 2019/7/5 16:52
 */
public class AppPopupWidows<T> implements Runnable, PopupWindow.OnDismissListener {
    private final String TAG = "PopupWindow";
    public Activity activity;
    private View mPopupLayout, mParentView;
    private PopupWindow mPopupWindow;
    private int mWidth, mHeight;
    private InitListener<T> mInitListener;
    private PopupWindow.OnDismissListener mOnDismissListener;
    private int mAnimStyle = -1;

    // ViewHolder
    private T mT;


    public interface InitListener<T> extends View.OnClickListener {
        T bindView(View v);

        void initViewData(T data, View v);
    }

    public AppPopupWidows(Activity activity, int layoutId) {
        this.activity = activity;
        mPopupLayout = View.inflate(activity, layoutId, null);
    }

    public AppPopupWidows(Activity activity, View view) {
        this.activity = activity;
        mPopupLayout = view;
    }

    public AppPopupWidows<T> setWidth(int dp) {
        mWidth = UIUtil.dip2px(dp);
        return this;
    }


    //设置占据屏幕高 70%
    public AppPopupWidows<T> setHeight(String dp) {
        int proportion = Integer.parseInt(dp.replace("%", ""));
        DisplayMetrics dm = activity.getResources().getDisplayMetrics();
        int heigth = dm.heightPixels;
        mHeight = (heigth / 100) * proportion;
        return this;

    }

    public AppPopupWidows<T> setHeight(int dp) {
        mHeight = UIUtil.dip2px(dp);
        return this;
    }

    public AppPopupWidows<T> setHeightPx(int dp) {
        mHeight = dp;
        return this;
    }

    public AppPopupWidows<T> setParentView(View parentView) {
        mParentView = parentView;
        return this;
    }

    public PopupWindow getPopupWindow() {
        return mPopupWindow;
    }


    public AppPopupWidows<T> builder(InitListener<T> listener) {
        if (listener == null) {
            throw new NullPointerException("AppPopupWidows init listener is Null ");
        }

        mInitListener = listener;
        mWidth = mWidth == 0 ? getScreenDisplay(activity).widthPixels : mWidth;
        mHeight = mHeight == 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : mHeight;

        mT = listener.bindView(mPopupLayout);

        mPopupWindow = new PopupWindow(mPopupLayout, mWidth, mHeight, true);
        //设置触摸PopupWindow之外的区域能关闭PopupWindow
        mPopupWindow.setOutsideTouchable(true);
        //设置PopupWindow可触摸
        mPopupWindow.setTouchable(true);
        //设置PopupWindow获取焦点
        mPopupWindow.setFocusable(true);
        //设置popupWindow的背景,必须设置，否则PopupWindow不能隐藏
//        if (!isOutsideTouchable)
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        //防止虚拟键遮挡
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置动画
        mPopupWindow.setAnimationStyle(mAnimStyle);
        mPopupWindow.setOnDismissListener(this);
        return this;
    }

    public DisplayMetrics getScreenDisplay(Activity activity) {
        //创建用于获取屏幕尺寸、像素密度的对象
        Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
        //创建用于获取屏幕尺寸、像素密度等信息的对象
        DisplayMetrics outMetrics = new DisplayMetrics();
        defaultDisplay.getMetrics(outMetrics);
        return outMetrics;
    }

    //设置一个动画
    public AppPopupWidows<T> setAnimStyle(@StyleRes int animStyle) {
        mAnimStyle = animStyle;
        return this;
    }

    public AppPopupWidows<T> show() {
        return show(-1);
    }

    public AppPopupWidows<T> show(int gravity) {
        initListener();
        mPopupWindow.showAtLocation(
                mParentView == null ? activity.getWindow().getDecorView() : mParentView
                , gravity == -1 ? Gravity.BOTTOM : gravity
                , 0
                , 0);
        return this;
    }

    public AppPopupWidows<T> setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
        return this;
    }

    /**
     * 设置显示在v上方(以v的左边距为开始位置)
     * 注需要指定 Popup 高度
     */
    public AppPopupWidows<T> showUp(View v) {
        initListener();

        int[] location = new int[2];
        v.getLocationOnScreen(location);
        mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] - mPopupWindow.getHeight());
        return this;
    }

    //指定View下方
    public AppPopupWidows<T> showDown(View v) {
        initListener();
        mPopupWindow.showAsDropDown(v);
        return this;
    }

    //指定View下方
    public AppPopupWidows<T> showDown(View v, int x, int y) {
        initListener();
        mPopupWindow.showAsDropDown(v, x, y);
        return this;
    }


    public AppPopupWidows<T> setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) {
        mOnDismissListener = onDismissListener;
        return this;
    }

    private void initListener() {
        mInitListener.initViewData(mT, mPopupLayout);
    }

    @Override
    public void onDismiss() {
        if (mOnDismissListener != null) {
            mOnDismissListener.onDismiss();
        }
    }

    public void dismiss() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
    }

    @Override
    public void run() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
    }

    /**
     * 是否可见
     *
     * @return 可见/不可见
     */
    public boolean isShow() {
        return mPopupWindow != null && mPopupWindow.isShowing();
    }


    public T getViewHoder() {
        return mT;
    }
}
